package com.zxl.live.tools.guide;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.zxl.live.tools.R;
import com.zxl.live.tools.common.ApplicationHelper;
import com.zxl.live.tools.tools.AppUtils;
import com.zxl.live.tools.tools.ProcessUtils;

import java.util.List;

/**
 * 顶部应用程序监听权限
 * Created by Administrator on 2016/10/26.
 */
public class UsagePermissionGuide extends BasePermissionGuide {

    private static final int US_AGE_STATS_UNKNOWN = -1;

    private static final int US_AGE_STATS_HAS_ACTIVITY = 1;

    private static final int US_AGE_STATS_HAS_NO_ACTIVITY = 2;

    private int status = US_AGE_STATS_UNKNOWN;

    private CustomToast mToast;

    public UsagePermissionGuide(Context mContext, Class mStartActivityCls) {
        super(mContext, mStartActivityCls);
    }

    @Override
    public boolean havePermission() {
        return !ProcessUtils.needStartUsageStats(ApplicationHelper.getContext());
    }

    @Override
    public void stopMonitor() {
        super.stopMonitor();
        if (null != mToast) {
            mToast.dismiss();
        }
    }

    /**
     * 是否需要显示授权页面
     * @return
     */
    public boolean startUsagePage() {
        if (ProcessUtils.needStartUsageStats(mContext)) {
            final boolean hasUsageActivity = hasUsageActivity(mContext);
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            if (!hasUsageActivity) {
                intent = new Intent(Settings.ACTION_SETTINGS);
            }
            if (!(mContext instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            try {
                mContext.startActivity(intent);
                startMonitor();
                ApplicationHelper.postThreadDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showUsageToast(hasUsageActivity);
                    }
                }, 200);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 是否有授权页面
     * @param context
     * @return
     */
    private boolean hasUsageActivity(Context context) {
        if (status == US_AGE_STATS_UNKNOWN) {
            Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
            if (resolveInfos == null || resolveInfos.isEmpty()) {
                status = US_AGE_STATS_HAS_NO_ACTIVITY;
            } else {
                status = US_AGE_STATS_HAS_ACTIVITY;
            }
        }
        if (status == US_AGE_STATS_HAS_ACTIVITY) {
            return true;
        } else if (status == US_AGE_STATS_HAS_NO_ACTIVITY) {
            return false;
        }
        return false;
    }

    private void showUsageToast(boolean hasUsageActivity) {
        mToast = new CustomToast();
        mToast.setToastSize(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        mToast.setWindowAnim(R.style.toast_guide_permission);

        View view = LayoutInflater.from(ApplicationHelper.getContext()).inflate(R.layout.toast_permission_alert_setting, null, false);
        ((ImageView) view.findViewById(R.id.iv_icon)).setImageDrawable(AppUtils.getAppIcon(mContext, mContext.getPackageName()));
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mToast.dismiss();
            }
        });
//        View view;
//        if (hasUsageActivity) {
//            view = LayoutInflater.from(ApplicationHelper.getContext()).inflate(R.layout.toast_permission_guide, null, false);
//        } else {
//            view = LayoutInflater.from(ApplicationHelper.getContext()).inflate(R.layout.toast_permission_guide_not_jump, null, false);
//        }
//        ((ImageView) view.findViewById(R.id.iv_icon)).setImageDrawable(AppUtils.getAppIcon(mContext, mContext.getPackageName()));
//        view.findViewById(R.id.iv_permission_guide_close).setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                mToast.dismiss();
//            }
//        });
        mToast.setGravity(Gravity.BOTTOM);
        mToast.setShowToastDuration(5000);
        mToast.setContentView(view);
        mToast.show();
    }

}
